package com.ruoyi.gczl.bzgf.dq.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gczl.bzgf.dq.domain.Bzgfdq;
import com.ruoyi.gczl.bzgf.dq.service.IBzgfdqService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 标准规范电气Controller
 *
 * @author huacai
 * @date 2024-08-28
 */
@RestController
@RequestMapping("/bzgf/bzgfdq")
public class BzgfdqController extends BaseController {
    @Resource
    private IBzgfdqService bzgfdqService;

    /**
     * 查询标准规范电气列表
     */
    @PreAuthorize("@ss.hasPermi('bzgf:bzgfdq:list')")
    @GetMapping("/list")
    public TableDataInfo list(Bzgfdq bzgfdq) {
        startPage();
        List<Bzgfdq> list = bzgfdqService.selectBzgfdqList(bzgfdq);
        return getDataTable(list);
    }

    /**
     * 导出标准规范电气列表
     */
    @PreAuthorize("@ss.hasPermi('bzgf:bzgfdq:export')")
    @Log(title = "标准规范电气", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Bzgfdq bzgfdq) {
        List<Bzgfdq> list = bzgfdqService.selectBzgfdqList(bzgfdq);
        ExcelUtil<Bzgfdq> util = new ExcelUtil<Bzgfdq>(Bzgfdq.class);
        util.exportExcel(response, list, "标准规范电气数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Bzgfdq> util = new ExcelUtil<Bzgfdq>(Bzgfdq.class);
        util.importTemplateExcel(response, "标准规范电气数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "标准规范电气", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('bzgf:bzgfdq:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Bzgfdq> util = new ExcelUtil<Bzgfdq>(Bzgfdq.class);
        InputStream inputStream = file.getInputStream();
        List<Bzgfdq> list = util.importExcel(inputStream);
        inputStream.close();
        int count = bzgfdqService.batchInsertBzgfdq(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取标准规范电气详细信息
     */
    @PreAuthorize("@ss.hasPermi('bzgf:bzgfdq:query')")
    @GetMapping(value = "/{bzgfdqId}")
    public AjaxResult getInfo(@PathVariable("bzgfdqId") Integer bzgfdqId) {
        return success(bzgfdqService.selectBzgfdqByBzgfdqId(bzgfdqId));
    }

    /**
     * 新增标准规范电气
     */
    @PreAuthorize("@ss.hasPermi('bzgf:bzgfdq:add')")
    @Log(title = "标准规范电气", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Bzgfdq bzgfdq) {
        bzgfdq.setUserId(Math.toIntExact(getUserId()));
        bzgfdq.setDeptId(Math.toIntExact(getDeptId()));
        bzgfdq.setCreateBy(getUsername());

        return toAjax(bzgfdqService.insertBzgfdq(bzgfdq));
    }

    /**
     * 修改标准规范电气
     */
    @PreAuthorize("@ss.hasPermi('bzgf:bzgfdq:edit')")
    @Log(title = "标准规范电气", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Bzgfdq bzgfdq) {
        return toAjax(bzgfdqService.updateBzgfdq(bzgfdq));
    }

    /**
     * 删除标准规范电气
     */
    @PreAuthorize("@ss.hasPermi('bzgf:bzgfdq:remove')")
    @Log(title = "标准规范电气", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bzgfdqIds}")
    public AjaxResult remove(@PathVariable Integer[] bzgfdqIds) {
        return toAjax(bzgfdqService.deleteBzgfdqByBzgfdqIds(bzgfdqIds));
    }
}
